#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPixmap>
#include <QAction>
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //设置标题
    this->setWindowTitle("老杨带你翻金币");
    //设置固定大小
    this->setFixedSize(320,588);
    //设置应用图片
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));


    //设置点击退出菜单，退出程序
    connect(ui->actionexit,&QAction::triggered,[=](){
        this->close();//关闭窗口
    });


    //创建开始按钮
    MyPushButton *startBtn = new MyPushButton(":/res/MenuSceneStartButton.png");
    startBtn->setParent(this);
    startBtn->move(this->width()*0.5-startBtn->width()*0.5,this->height()*0.7);

    //连接信号槽，监听开始按钮点击
    connect(startBtn,&MyPushButton::clicked,[=](){
        startSound->play();//开始音效
        startBtn->zoom1();//向下跳
        startBtn->zoom2();//向上跳

        //延迟0.5秒后，进入选择场景
        QTimer::singleShot(500,this,[=](){
            this->hide();
            chooseScene->setGeometry(this->geometry());
            chooseScene->show();
        });

    });


    //监听选择场景的返回按钮
    connect(chooseScene,&ChooseLevelScene::chooseSceneBack,[=](){
        this->setGeometry(chooseScene->geometry());
        this->show();
    });

}

MainWindow::~MainWindow()
{
    delete ui;
}

/**
  重写绘图事件绘制window背景
 * @brief MainWindow::paintEvent
 * @param event
 *
 */
void MainWindow::paintEvent(QPaintEvent *event){
    //创建画家指定绘图设备
    QPainter painter(this);
    //创建QPixmap对象
    QPixmap pixmap;
    //加载图片
    pixmap.load(":/res/PlayLevelSceneBg.png");
    //绘制背景
    painter.drawPixmap(0,0,this->width(),this->height(),pixmap);

    //加载标题
    pixmap.load(":/res/Title.png");
    //缩放图片
    pixmap = pixmap.scaled(pixmap.width()*0.5,pixmap.height()*0.5);
    //绘制标题
    painter.drawPixmap(10,30,pixmap.width(),pixmap.height(),pixmap);

}

